<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel='stylesheet' href='../rurple.css' type='text/css'>
<title>32. Many returns</title>
</head>
<body>

<h2 class="title">32. Many <span class="pykeyword">return</span>s</h2>
<p>As you know, Reeborg is not exactly in good shape.  He can only turn left, has an oil leak, can only see walls when they are right next to him and hear beepers when he is literally standing on top of them.  Reeborg has also a (somewhat broken) compass which he can used to find out if is is facing north ... or not. To find out if he is facing north, you can ask Reeborg to do the test <tt>facing_north()</tt>. </p> 
<!--==============================-->
<hr width="50%">
<h3 class="try">Try it!</h3>
<p>Write a short program that will ensure that Reeborg will turn left until he faces north and then turn itself off, no matter what his starting orientation is.</p>

<!--==============================-->
<hr width="50%">
<h3 class="section">Getting results from a function</h3>
<p>The tests that Reeborg performs are actually Python functions.  The result of these functions is not to print out a value, like we have seen so far, but rather to obtain an answer (True or False in this case) that can be used afterwards.  To do something similar ourselves, we need to use the Python keyword <span class="pykeyword">return</span>.  Here's an example:</p>
<pre>
&gt;&gt;&gt; <span class="keyword">def</span> add(a, b):
...     answer = a + b
...     <span class="keyword">return</span> answer
...
&gt;&gt;&gt; c = add(4, 5)
&gt;&gt;&gt; <span class="keyword">print</span> c
9
&gt;&gt;&gt; <span class="comment"># We can print directly the output of the function.</span>
&gt;&gt;&gt; <span class="keyword">print</span> add(1, 1)
2
</pre>
<p>In the above example, we could have defined the function <tt>add()</tt> in one less line of code, as follows:</p>
<pre>
&gt;&gt;&gt; <span class="keyword">def</span> add(a, b):
...     <span class="keyword">return</span> a + b
...
&gt;&gt;&gt; c = add(4, 5)
&gt;&gt;&gt; <span class="keyword">print</span> c
9
</pre>
<p>Thus <span class="pykeyword">return</span> can "return" the result of any valid Python expression, not simply the value of a variable.  If the expression you want to "return" is short, you might not want to use an extra variable (like <tt>answer</tt> above).  However, as you will see later, you can sometime "return" more than one variable... Then, it may be easier to read if we use variable names instead of Python expressions.</p>
<!--==============================-->
<hr width="50%">
<h3 class="section"><span class="pykeyword">return</span>ing to Reeborg's world</h3>

<p>As we mentioned at the beginning of this lesson, Reeborg has a broken compass.  As a result, he only knows directly if he is facing north, or if he isn't.  However, now that we know that Reeborg can remember things, we can teach him to find out which direction he is facing. For example: </p>
<pre>
<span class="keyword">def</span> facing_south():
    turn_left()
    turn_left()
    answer = facing_north()
    turn_left()
    turn_left()
    <span class="keyword">return</span> answer
</pre>
<p>Let's see how this work, by examining two cases:</p>
<ul><li><b>Suppose that Reeborg is facing south.</b> After two left turns, he is left facing north.  The test <tt>facing_north()</tt> gives the value "<tt>True</tt>" which is assigned to the variable <tt>answer</tt>.  After two more left turns, Reeborg is facing south again (back to its original orientation), and we return the answer "<tt>True</tt>"!</li>
<li><b>Suppose that Reeborg is <u>not</u> facing south.</b> Then, after two left turns, he is <u>not</u> facing north, and the test <tt>facing_north()</tt> gives the value "<tt>False</tt>" which is assigned to the variable <tt>answer</tt>.  After two more left turns, Reeborg is facing south again (back to its original orientation), and we return the answer "<tt>False</tt>"!</li>
</ul>
<p>So, we can use our new test if we want to have Reeborg face south:</p>
<pre>
<span class="keyword">while</span> <span class="keyword">not</span> facing_south():
    turn_left()
</pre>
<!--==============================-->
<hr width="50%">
<h3 class="try">Mini-quiz</h3>
<p>Write a program that has Reeborg face west, no matter what his orginal orientation is.  Test it with Reeborg in various starting orientation.</p>
<!--==============================-->
<hr width="50%">
<h3 class="try">Mini-quiz 2</h3>
<p>Place Reeborg in an arbitrary place, facing an arbitrary direction in an empty world. Write a program that has Reeborg come back to the origin, his usual starting point, facing east.  The same program should work regardless of the starting position and orientation.</p>

<center><a href="31-global.htm"><img alt="previous" src=
"../../images/previous.png">Globalization is a bad thing</a> - <a href=
"../lessons_toc.htm"><img alt="home" src="../../images/home.png"></a> - <a href=
"33-for.htm">Repeat()is hiding some Python keywords<img alt="next"
src="../../images/next.png"></a></center>

</body>
</html>